//
//  MySceneViewController.m
//  MyProject
//
//  Created by Pei on 16/6/19.
//  Copyright © 2016年 Xiaobo Pei. All rights reserved.
//

#import "MySceneViewController.h"
#import "TaskManagerViewController.h"
#import "StatisticsViewController.h"
#import "CreateTaskViewController.h"
#import "ToBeSureTaskListViewController.h"
#import "ToBeChangeTaskListViewController.h"
#import "ToBeReviewedTaskListViewController.h"
#import "ListViewController.h"
#import "ProjectStatisticsViewController.h"
#import "UserInfo.h"
#import "UIImageView+WebCache.h"

@interface MySceneViewController ()
{
    UIImageView *introImageView;
}

@end

@implementation MySceneViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [[NSNotificationCenter defaultCenter] postNotificationName:Not_Hide_TabBar_Notification object:nil];
}

- (void)viewDidLoad {
    
    self.navTitle = @"现场检查";
    
    [super viewDidLoad];
    
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 22)];
    [rightBtn setTitle:@"创建" forState:UIControlStateNormal];
    [rightBtn setTitleColor:kBlueColor forState:UIControlStateNormal];
    rightBtn.titleLabel.font = kAppFont(Home_Big_Size);
    [rightBtn addTarget:self action:@selector(rightBtnAcion) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:rightBtn]];
    
    [self initTopView];
    
    [self initContentView];
}

#pragma mark ************************ UI ***************************
- (void)initTopView
{
    introImageView = [[UIImageView alloc] init];
    //introImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"01banner"]];
    introImageView.frame = CGRectMake(0.0f, 0.0f, kDeviceWidth, 330.0f/2.0f);
    NSString *urlStr = kAppGetHeaderImage(@"SQCP_App_security_header");
    [introImageView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"normalDefault"]];
    [self.view addSubview:introImageView];
}

- (void)initContentView
{
    UIView *contentView = [[UIView alloc] init];
    contentView.frame = CGRectMake(0.0f, introImageView.frame.size.height+15.0f, kDeviceWidth, kDeviceHeight-kTabHeight-NAV_HEIGHT-introImageView.frame.size.height-15.0f-30.0f);
    contentView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:contentView];
    
    float cWidth = (kDeviceWidth-15*2-10)/2.0f;
    float cHeight = (contentView.frame.size.height-10.0f*2.0f)/6.0f;
    
    UIControl *topLeft = [[UIControl alloc] init];
    topLeft.frame = CGRectMake(15.0f, 0.0f, cWidth, cHeight*3.0f);
    //待检查任务:ToBeInspectedInspection
    if ([self isHasUserPrivilege:@"ToBeInspectedInspection"]) {
        topLeft.backgroundColor = kRGB(44, 176, 240);
    }else{
        topLeft.backgroundColor = kNoUserPrivilegeColor;
    }
    //topLeft.backgroundColor = kRGB(44, 176, 240);
    topLeft.layer.cornerRadius = 5.0f;
    topLeft.layer.masksToBounds = YES;
    topLeft.tag = 166;
    [topLeft addTarget:self action:@selector(senderAction:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:topLeft];
    
    UIImageView *topLeftImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"02daijiancha"]];
    if (iPhone4)
    {
        topLeftImageView.frame = CGRectMake(20.0f, (topLeft.frame.size.height-86/3-5*2-Home_Small_Size-(Home_Small_Size+2.0f))/2.0f, 90/3.0f, 86.0f/3.0f);
    }
    else
    {
        topLeftImageView.frame = CGRectMake(20.0f, (topLeft.frame.size.height-86/2-5*2-Home_Small_Size-Home_Big_Size)/2.0f, 90/2.0f, 86.0f/2.0f);
    }
    
    [topLeft addSubview:topLeftImageView];
    
    UILabel *topLeftTitleLbl = [[UILabel alloc] init];
    //topLeftTitleLbl.frame = CGRectMake(topLeftImageView.frame.origin.x, topLeftImageView.frame.origin.y+topLeftImageView.frame.size.height+5.0f, topLeft.frame.size.width-20.0f*2.0f, Home_Big_Size);
    topLeftTitleLbl.text = @"待检查任务";
    //topLeftTitleLbl.font = kAppFont(Home_Big_Size);
    if (iPhone4)
    {
        topLeftTitleLbl.frame = CGRectMake(topLeftImageView.frame.origin.x, topLeftImageView.frame.origin.y+topLeftImageView.frame.size.height+5.0f, topLeft.frame.size.width-20.0f*2.0f, Home_Small_Size+2.0f);
        topLeftTitleLbl.font = kAppFont(Home_Small_Size+2.0f);
    }
    else
    {
        topLeftTitleLbl.frame = CGRectMake(topLeftImageView.frame.origin.x, topLeftImageView.frame.origin.y+topLeftImageView.frame.size.height+3.0f, topLeft.frame.size.width-20.0f*2.0f, Home_Big_Size);
        topLeftTitleLbl.font = kAppFont(Home_Big_Size);
    }
    topLeftTitleLbl.textColor = kWhiteColor;
    [topLeft addSubview:topLeftTitleLbl];
    
    UILabel *topLeftDetailLbl = [[UILabel alloc] init];
    topLeftDetailLbl.text = @"各类待办事项";
    if (iPhone4)
    {
        topLeftDetailLbl.frame = CGRectMake(topLeftImageView.frame.origin.x, topLeftTitleLbl.frame.origin.y+topLeftTitleLbl.frame.size.height+3.0f, topLeft.frame.size.width-20.0f*2.0f, Home_Small_Size);
        topLeftDetailLbl.font = kAppFont(Home_Small_Size);
    }
    else
    {
        topLeftDetailLbl.frame = CGRectMake(topLeftImageView.frame.origin.x, topLeftTitleLbl.frame.origin.y+topLeftTitleLbl.frame.size.height+5.0f, topLeft.frame.size.width-20.0f*2.0f, Home_Small_Size);
        topLeftDetailLbl.font = kAppFont(Home_Small_Size);
    }
    topLeftDetailLbl.textColor = kWhiteColor;
    [topLeft addSubview:topLeftDetailLbl];
    
    
    UIControl *centerLeft = [[UIControl alloc] init];
    centerLeft.frame = CGRectMake(15.0f, topLeft.frame.size.height+10.0f, cWidth, cHeight*2.0f);
    //待整改任务:ToBeRepairedInspection
    if ([self isHasUserPrivilege:@"ToBeRepairedInspection"]) {
        centerLeft.backgroundColor = kRGB(73, 151, 252);
    }else{
        centerLeft.backgroundColor = kNoUserPrivilegeColor;
    }
    //centerLeft.backgroundColor = kRGB(73, 151, 252);
    centerLeft.layer.cornerRadius = 5.0f;
    centerLeft.layer.masksToBounds = YES;
    centerLeft.tag = 168;
    [centerLeft addTarget:self action:@selector(senderAction:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:centerLeft];
    
    
    UIImageView *centerLeftImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"04daizhenggai"]];
    if (iPhone4)
    {
        centerLeftImageView.frame = CGRectMake(10.0f, (centerLeft.frame.size.height-70/3)/2.0f, 80/3.0f, 70.0f/3.0f);
    }
    else
    {
       centerLeftImageView.frame = CGRectMake(10.0f, (centerLeft.frame.size.height-70/2)/2.0f, 80/2.0f, 70.0f/2.0f);
    }
    [centerLeft addSubview:centerLeftImageView];
    
    UILabel *centerLeftTitleLbl = [[UILabel alloc] init];
    centerLeftTitleLbl.text = @"待整改任务";
    if (iPhone4)
    {
        centerLeftTitleLbl.frame = CGRectMake(centerLeftImageView.frame.origin.x+centerLeftImageView.frame.size.width+5.0f, centerLeftImageView.frame.origin.y-5.0f, centerLeft.frame.size.width-10.0f-centerLeftImageView.frame.size.width, Home_Small_Size+2.0f);
        centerLeftTitleLbl.font = kAppFont(Home_Small_Size+2.0f);
    }
    else
    {
        centerLeftTitleLbl.frame = CGRectMake(centerLeftImageView.frame.origin.x+centerLeftImageView.frame.size.width+5.0f, centerLeftImageView.frame.origin.y, centerLeft.frame.size.width-10.0f-centerLeftImageView.frame.size.width, Home_Big_Size);
        centerLeftTitleLbl.font = kAppFont(Home_Big_Size);
    }
    centerLeftTitleLbl.textColor = kWhiteColor;
    [centerLeft addSubview:centerLeftTitleLbl];
    
    UILabel *centerLeftDetailLbl = [[UILabel alloc] init];
    centerLeftDetailLbl.frame = CGRectMake(centerLeftTitleLbl.frame.origin.x, centerLeftTitleLbl.frame.origin.y+centerLeftTitleLbl.frame.size.height+5.0f, centerLeftTitleLbl.frame.size.width, Home_Small_Size);
    centerLeftDetailLbl.text = @"整修，整改";
    centerLeftDetailLbl.font = kAppFont(Home_Small_Size);
    centerLeftDetailLbl.textColor = kWhiteColor;
    [centerLeft addSubview:centerLeftDetailLbl];
    
    
    
    
    UIControl *bottomLeft = [[UIControl alloc] init];
    bottomLeft.frame = CGRectMake(15.0f, centerLeft.frame.origin.y+centerLeft.frame.size.height+10.0f, cWidth, cHeight);
    //公司统计: CompanyAnalysis
    if ([self isHasUserPrivilege:@"CompanyAnalysis"]) {
        bottomLeft.backgroundColor = kRGB(41, 131, 252);
    }else{
        bottomLeft.backgroundColor = kNoUserPrivilegeColor;
    }
    //bottomLeft.backgroundColor = kRGB(41, 131, 252);
    bottomLeft.layer.cornerRadius = 5.0f;
    bottomLeft.layer.masksToBounds = YES;
    bottomLeft.tag = 169;
    [bottomLeft addTarget:self action:@selector(senderAction:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:bottomLeft];
    
    UILabel *bottomLeftTitleLbl = [[UILabel alloc] init];
    bottomLeftTitleLbl.text = @"公司统计";
    if (iPhone4)
    {
        bottomLeftTitleLbl.frame = CGRectMake(0.0f, (bottomLeft.frame.size.height-Home_Big_Size)/2.0f, bottomLeft.frame.size.width, Home_Small_Size+2.0f);
        bottomLeftTitleLbl.font = kAppFont(Home_Small_Size+2.0f);
    }
    else
    {
        bottomLeftTitleLbl.frame = CGRectMake(0.0f, (bottomLeft.frame.size.height-Home_Big_Size)/2.0f, bottomLeft.frame.size.width, Home_Big_Size);
        bottomLeftTitleLbl.font = kAppFont(Home_Big_Size);
    }
    bottomLeftTitleLbl.textColor = kWhiteColor;
    bottomLeftTitleLbl.textAlignment = NSTextAlignmentCenter;
    [bottomLeft addSubview:bottomLeftTitleLbl];
    
    
    UIControl *topRight = [[UIControl alloc] init];
    topRight.frame = CGRectMake(topLeft.frame.origin.x+topLeft.frame.size.width+10.0f, 0.0f, cWidth, cHeight*2.0f);
    //待确认任务:ToBeConfirmedInspection
    if ([self isHasUserPrivilege:@"ToBeConfirmedInspection"]) {
        topRight.backgroundColor = kRGB(41, 131, 252);
    }else{
        topRight.backgroundColor = kNoUserPrivilegeColor;
    }
    //topRight.backgroundColor = kRGB(41, 131, 252);
    topRight.layer.cornerRadius = 5.0f;
    topRight.layer.masksToBounds = YES;
    topRight.tag = 186;
    [topRight addTarget:self action:@selector(senderAction:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:topRight];
    
    UIImageView *topRightImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"03daiqueren"]];
    if (iPhone4)
    {
        topRightImageView.frame = CGRectMake(10.0f, (centerLeft.frame.size.height-70/3)/2.0f, 80/3.0f, 70.0f/3.0f);
    }
    else
    {
        topRightImageView.frame = CGRectMake(10.0f, (centerLeft.frame.size.height-70/2)/2.0f, 80/2.0f, 70.0f/2.0f);
    }
    [topRight addSubview:topRightImageView];
    
    UILabel *topRightTitleLbl = [[UILabel alloc] init];
    topRightTitleLbl.text = @"待确认任务";
    if (iPhone4)
    {
        topRightTitleLbl.frame = CGRectMake(topRightImageView.frame.origin.x+topRightImageView.frame.size.width+5.0f, topRightImageView.frame.origin.y-5.0f, centerLeft.frame.size.width-10.0f-topRightImageView.frame.size.width, Home_Small_Size+2.0f);
        topRightTitleLbl.font = kAppFont(Home_Small_Size+2.0f);
    }
    else
    {
        topRightTitleLbl.frame = CGRectMake(topRightImageView.frame.origin.x+topRightImageView.frame.size.width+5.0f, topRightImageView.frame.origin.y, centerLeft.frame.size.width-10.0f-topRightImageView.frame.size.width, Home_Big_Size);
        topRightTitleLbl.font = kAppFont(Home_Big_Size);
    }
    topRightTitleLbl.textColor = kWhiteColor;
    [topRight addSubview:topRightTitleLbl];
    
    UILabel *topRightDetailLbl = [[UILabel alloc] init];
    topRightDetailLbl.frame = CGRectMake(topRightTitleLbl.frame.origin.x, topRightTitleLbl.frame.origin.y+topRightTitleLbl.frame.size.height+5.0f, topRightTitleLbl.frame.size.width, Home_Small_Size);
    topRightDetailLbl.text = @"需要确认";
    topRightDetailLbl.font = kAppFont(Home_Small_Size);
    topRightDetailLbl.textColor = kWhiteColor;
    [topRight addSubview:topRightDetailLbl];
    
    
    
    
    
    UIControl *centerRight = [[UIControl alloc] init];
    centerRight.frame = CGRectMake(topRight.frame.origin.x, topRight.frame.origin.y+topRight.frame.size.height+10.0f, cWidth, cHeight*3.0f);
    //待审核任务:ToBeAuditedInspection
    if ([self isHasUserPrivilege:@"ToBeAuditedInspection"]) {
        centerRight.backgroundColor = kRGB(44, 176, 240);
    }else{
        centerRight.backgroundColor = kNoUserPrivilegeColor;
    }
    //centerRight.backgroundColor = kRGB(44, 176, 240);
    centerRight.layer.cornerRadius = 5.0f;
    centerRight.layer.masksToBounds = YES;
    centerRight.tag = 188;
    [centerRight addTarget:self action:@selector(senderAction:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:centerRight];
    
    UIImageView *centerRightImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"05daishenhe"]];
    if (iPhone4)
    {
       centerRightImageView.frame = CGRectMake(20.0f, (centerRight.frame.size.height-86/3-5*2-Home_Small_Size-(Home_Small_Size+2.0f))/2.0f, 90/3.0f, 86.0f/3.0f);
    }
    else
    {
       centerRightImageView.frame = CGRectMake(20.0f, (centerRight.frame.size.height-86/2-5*2-Home_Small_Size-Home_Big_Size)/2.0f, 90/2.0f, 86.0f/2.0f);
    }
    [centerRight addSubview:centerRightImageView];
    
    UILabel *centerRightTitleLbl = [[UILabel alloc] init];
    centerRightTitleLbl.text = @"待审核任务";
    if (iPhone4)
    {
        centerRightTitleLbl.frame = CGRectMake(centerRightImageView.frame.origin.x, centerRightImageView.frame.origin.y+centerRightImageView.frame.size.height+3.0f, centerRight.frame.size.width-20.0f*2.0f, Home_Small_Size+2.0f);
        centerRightTitleLbl.font = kAppFont(Home_Small_Size+2.0f);
        
    }
    else
    {
        centerRightTitleLbl.frame = CGRectMake(centerRightImageView.frame.origin.x, centerRightImageView.frame.origin.y+centerRightImageView.frame.size.height+5.0f, centerRight.frame.size.width-20.0f*2.0f, Home_Big_Size);
        centerRightTitleLbl.font = kAppFont(Home_Big_Size);
    }
    centerRightTitleLbl.textColor = kWhiteColor;
    [centerRight addSubview:centerRightTitleLbl];
    
    UILabel *centerRightDetailLbl = [[UILabel alloc] init];
    centerRightDetailLbl.text = @"事务型";
    if (iPhone4)
    {
        centerRightDetailLbl.frame = CGRectMake(centerRightImageView.frame.origin.x, centerRightTitleLbl.frame.origin.y+centerRightTitleLbl.frame.size.height+3.0f, centerRight.frame.size.width-20.0f*2.0f, Home_Small_Size);
        centerRightDetailLbl.font = kAppFont(Home_Small_Size);
    }
    else
    {
        centerRightDetailLbl.frame = CGRectMake(centerRightImageView.frame.origin.x, centerRightTitleLbl.frame.origin.y+centerRightTitleLbl.frame.size.height+5.0f, centerRight.frame.size.width-20.0f*2.0f, Home_Small_Size);
        centerRightDetailLbl.font = kAppFont(Home_Small_Size);
    }
    centerRightDetailLbl.textColor = kWhiteColor;
    [centerRight addSubview:centerRightDetailLbl];
    
    
    
    
    UIControl *bottomRight = [[UIControl alloc] init];
    bottomRight.frame = CGRectMake(topRight.frame.origin.x, centerRight.frame.origin.y+centerRight.frame.size.height+10.0f, cWidth, cHeight);
    //项目统计: ProjectAnalysis
    if ([self isHasUserPrivilege:@"ProjectAnalysis"]) {
        bottomRight.backgroundColor = kRGB(41, 131, 252);
    }else{
        bottomRight.backgroundColor = kNoUserPrivilegeColor;
    }
    //bottomRight.backgroundColor = kRGB(41, 131, 252);
    bottomRight.layer.cornerRadius = 5.0f;
    bottomRight.layer.masksToBounds = YES;
    bottomRight.tag = 189;
    [bottomRight addTarget:self action:@selector(senderAction:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:bottomRight];
    
    UILabel *bottomRightTitleLbl = [[UILabel alloc] init];
    bottomRightTitleLbl.text = @"项目统计";
    
    if (iPhone4)
    {
        bottomRightTitleLbl.frame = CGRectMake(0.0f, (bottomRight.frame.size.height-Home_Big_Size)/2.0f, bottomRight.frame.size.width, Home_Small_Size+2.0f);
        bottomRightTitleLbl.font = kAppFont(Home_Small_Size+2.0f);
    }
    else
    {
        bottomRightTitleLbl.frame = CGRectMake(0.0f, (bottomRight.frame.size.height-Home_Big_Size)/2.0f, bottomRight.frame.size.width, Home_Big_Size);
        bottomRightTitleLbl.font = kAppFont(Home_Big_Size);
    }
    bottomRightTitleLbl.textColor = kWhiteColor;
    bottomRightTitleLbl.textAlignment = NSTextAlignmentCenter;
    [bottomRight addSubview:bottomRightTitleLbl];

}

#pragma mark ***************************** Action *********************************
- (void)senderAction:(UIControl*)sender
{
    switch (sender.tag) {
        case 166://待检查
        {
            //ToBeInspectedInspection
            if ([self isHasUserPrivilege:@"ToBeInspectedInspection"]) {
                TaskManagerViewController *taskVC = [[TaskManagerViewController alloc] initWith:MySceneToBeCheck];
                [self.navigationController pushViewController:taskVC animated:YES];
            }else{
                [self showAlertView];
            }
            
        }
            break;
        case 168://待整改
        {
            //待整改任务:ToBeRepairedInspection
            if ([self isHasUserPrivilege:@"ToBeRepairedInspection"]) {
                ToBeChangeTaskListViewController *changeVC = [[ToBeChangeTaskListViewController alloc] init];
                changeVC.navTitle = @"待整改任务";
                [self.navigationController pushViewController:changeVC animated:YES];
            }else{
                [self showAlertView];
            }
            //TaskManagerViewController *taskVC = [[TaskManagerViewController alloc] initWith:MySceneToBeChange];
            //[self.navigationController pushViewController:taskVC animated:YES];
            
        }
            break;
        case 169:
        {
            //公司统计: CompanyAnalysis
            if ([self isHasUserPrivilege:@"CompanyAnalysis"]) {
                StatisticsViewController *statisticsVC = [[StatisticsViewController alloc] initWithType:@"0"];
                statisticsVC.navTitle = @"公司统计";
                [self.navigationController pushViewController:statisticsVC animated:YES];
            }else{
                [self showAlertView];
            }
            
        }
            break;
        case 186://待确认
        {
            //待确认任务:ToBeConfirmedInspection
            if ([self isHasUserPrivilege:@"ToBeConfirmedInspection"]) {
                ToBeSureTaskListViewController *toBeVC = [[ToBeSureTaskListViewController alloc] init];
                toBeVC.navTitle = @"待确认检查单";
                [self.navigationController pushViewController:toBeVC animated:YES];
            }else{
                [self showAlertView];
            }
            //TaskManagerViewController *taskVC = [[TaskManagerViewController alloc] initWith:MySceneToBeSure];
            //[self.navigationController pushViewController:taskVC animated:YES];
            
        }
            break;
        case 188://待审核
        {
            //待审核任务:ToBeAuditedInspection
            if ([self isHasUserPrivilege:@"ToBeAuditedInspection"]) {
                ToBeReviewedTaskListViewController *taskVC = [[ToBeReviewedTaskListViewController alloc] init];
                [self.navigationController pushViewController:taskVC animated:YES];
            }else{
                [self showAlertView];
            }
            
        }
            break;
        case 189:
        {
            //项目统计: ProjectAnalysis
            if ([self isHasUserPrivilege:@"ProjectAnalysis"]) {
                ProjectStatisticsViewController *statisticsVC = [[ProjectStatisticsViewController alloc] initWithType:@"1"];
                statisticsVC.navTitle = @"项目统计";
                [self.navigationController pushViewController:statisticsVC animated:YES];
            }else{
                [self showAlertView];
            }
            
            
            //ListViewController *listVC = [[ListViewController alloc] init];
            //[self.navigationController pushViewController:listVC animated:YES];
        }
            break;
        default:
            break;
    }
}

- (void)rightBtnAcion
{
    CreateTaskViewController *createTaskVC = [[CreateTaskViewController alloc] init];
    [self.navigationController pushViewController:createTaskVC animated:YES];
}

- (void)showAlertView{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您没有此模块的权限" delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
    [alert show];
}

- (BOOL)isHasUserPrivilege:(NSString*)tempStr{
    NSArray *uuAry = [[UserInfo getInstance] getUserPrivilege];
    BOOL isExit = NO;
    for (int i = 0; i < uuAry.count; i ++) {
        NSString *str = [uuAry objectAtIndex:i];
        if ([str isEqualToString:tempStr]) {
            isExit = YES;
        }
    }
    
    if (isExit) {
        return YES;
    }else{
        return NO;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
